package com.thinkgem.jeesite.common.utils;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.thinkgem.jeesite.modules.config.entity.email.ConfigEmail;
import com.thinkgem.jeesite.modules.config.service.email.ConfigEmailService;

public class JavaMailSend {

    public void sendMail(String code,String toEamil){
        ConfigEmailService configEmailService =SpringContextHolder.getBean(ConfigEmailService.class);
        ConfigEmail ms = configEmailService.getConfigEmail();
        if(ms!=null){
            Properties props = new Properties();  
            // 开启debug调试  
          //  props.setProperty("mail.debug", "true");  
            // 发送服务器需要身份验证  
            props.setProperty("mail.smtp.auth", "true");  
            // 设置邮件服务器主机名  
            props.setProperty("mail.host", ms.getAddress());  
            // 发送邮件协议名称  
            props.setProperty("mail.transport.protocol", "smtp");  
              
            // 设置环境信息  
            Session session = Session.getInstance(props);  
              
            // 创建邮件对象  
            Message msg = new MimeMessage(session);
            
              
            Transport transport;
            try {
                msg.setSubject("霁圣科技");
                msg.setText("霁圣科技发送验证码，请绝对不让别人知道！"+code);
                msg.setFrom(new InternetAddress(ms.getAccount()));
                
                transport = session.getTransport();
                transport.connect(ms.getAccount(), ms.getPassword());
                transport.sendMessage(msg, new Address[] {new InternetAddress(toEamil)});
                transport.close();
            } catch (MessagingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }  
        }
     
    }
}
